package com.prs.crm.service.fs.impl;

import java.io.File;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.dao.fs.ApplyDealDao;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.fs.ApplyDeal;
import com.prs.crm.domain.fs.Detail;
import com.prs.crm.service.fs.ApplyDealService;
import com.prs.crm.service.fs.DetailService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class ApplyDealServiceImpl extends BaseServiceImpl<ApplyDeal,ApplyDealDao> implements ApplyDealService{
	private DetailService detailService;
	
	@Override
	public void submitApplyDeal(ApplyDeal applyDeal, File file, String fileName) {
		// TODO Auto-generated method stub
		Attachment attach = FileUtil.getInstance().createAttach(file, fileName);
		if(attach != null) {
			applyDeal.setAttach(attach);
		}
		this.getDao().save(applyDeal);
		
		Double sum = this.getDetailService().getBalanceByAccount(applyDeal.getAccount());
		Detail detial = new Detail();
		detial.setOutlayApply(applyDeal.getApply());
		detial.setIncome(0.0);
		detial.setPay(applyDeal.getApply().getMoney());
		detial.setBalance(sum - applyDeal.getApply().getMoney());
		detial.setBankAccount(applyDeal.getAccount());
		detial.setItem(Constrains.FS_ITEMS_OUTLAYAPPLY);		
		this.getDetailService().save(detial);
	}

	public void setDetailService(DetailService detailService) {
		this.detailService = detailService;
	}

	public DetailService getDetailService() {
		return detailService;
	}

}
